// pages/posts/addposts.js
var app = getApp();
const mixins = require('../../mixins/forum')
const common = require('../../mixins/common')
var a = wx.getSystemInfoSync(),
  n = a.windowWidth - 30 - 50,
  o = a.windowHeight - 100;
import {
  uploadMedia,
} from "../../api/common.js";
import config from "../../api/config";
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    format: 'standard',
    imagesubject: [],
    params: {
      ctxW: n,
      ctxH: o
    },
    topic: '日签',
    type: 'sign',
    currentConfig: config.currentConfig,
    tags: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var myDate = new Date();
    that.setData({
      year: myDate.getFullYear(),
      month: myDate.getMonth(),
      day: myDate.getDay(),
      date: myDate.getFullYear() + '-' + myDate.getMonth() + '-' + myDate.getDay()
    })
    wx.getSystemInfo({
      success: function (t) {
        that.setData({
          width: t.windowWidth - 30,
          height: t.windowHeight - 35
        });
      }
    });
  },

  chooseImage: function (t, e) {
    var s = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album", "camera"],
      success: function (a) {
        s.setData({
          tkShowAndCut: e,
          isShow: !1
        }), s.data.params.sourcePath = a.tempFilePaths[0], s.setData({
          params: s.data.params,
          acatarcutShow: !0,
          isSunInfo: t || null
        });
      },
      complete: {},
      fail: function (t) {}
    });
  },
  creatPicItem: function () {
    this.chooseImage(!0);
  },

  bindDateChange: function (e) {
    var a = 1e3 * (60 * (new Date().getHours() - 8) * 60 + 60 * new Date().getMinutes() + new Date().getSeconds());
    this.setData({
      date: e.detail.value
    })
  },

  onMyUploadpic: function (t) {
    var s = this;
    console.log(t), "success" == t.detail.msg ? (s.setData({
      acatarcutShow: !1
    }), wx.showLoading({
      title: "数据处理中"
    }), s.uploadImage(t.detail.res)) : wx.showToast({
      icon: "none",
      title: "裁剪失败，请重新裁剪"
    });
  },

  uploadImage: function (file) {
    var that = this;
    uploadMedia(file).then(res => {
      if (res.url) {
        wx.hideLoading();
        that.setData({
          acatarcutShow: !1,
          isShow: !0,
          imagesubject: res.url
        })
      }

    }).catch(err => {
      console.log(err)
      if (err.message == "未授权登录用户,不能上传") {
        that.data.currentConfig.show = true;
        that.setData({
          currentConfig: that.data.currentConfig,
        })
      }

      wx.showToast({
        title: '图片添加失败'
      })
    })
  },

  onContentInput: function (t) {
    var a = t.detail.value;
    a.length > 0 ? this.data.title = a : this.data.title = "";
  },

  creatItem: function () {
    this.setData({
      isShow: !0,
      isEdit: !1
    });
  },

  onTagClick: function () {
    wx.navigateTo({
      url: '/pages/creat/tags?tags=' + JSON.stringify(this.data.tags)
    })
  },


  onTopicClick: function () {
    wx.navigateTo({
      url: '/pages/creat/circle',
    })

  },


  onLocationClick: function () {
    var t = this;
    this.checkPermission("scope.userLocation", "请到设置页面授权“位置信息”权限", function () {
      t.startChooseLocation();
    });
  },

  closeItem: function () {
    this.setData({
      isShow: !1,
    });
  },

  InputFun: function (e) {
    this.setData({
      textinput: e.detail.value
    });
  },

  InputTitleFun: function (e) {
    this.setData({
      title: e.detail.value
    });
  },

  startChooseLocation: function () {
    var t = this;
    wx.chooseLocation({
      success: function (a) {
        t.setData({
          location: {
            latitude: a.latitude,
            longitude: a.longitude,
            address: a.address,
            name: a.name
          }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user = app.globalData.user
    if (!user) {
      wx.showToast({
        title: '未登录,请登录',
      })
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      user: user,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}

mixins(options)
common(options)
Page(options)